<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="请输入订单号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="phonenumber">
        <el-input v-model="queryParams.phonenumber" placeholder="请输入手机号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入用户名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="查询时间">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="YYYY-MM-DD" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="订单状态" prop="payStatus">
        <el-select @change="getList" v-model="queryParams.payStatus" placeholder="请选择订单状态" clearable>
          <el-option v-for="dict in pay_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="充电类型" prop="chargeType">
        <el-select @change="getList" v-model="queryParams.chargeType" placeholder="请选择充电类型" clearable>
          <el-option v-for="dict in charge_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="充电状态" prop="chargeStatus">
        <el-select @change="getList" v-model="queryParams.chargeStatus" placeholder="请选择充电状态" clearable>
          <el-option v-for="dict in charge_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="充电模式" prop="chargeMode">
        <el-select @change="getList" v-model="queryParams.chargeMode" placeholder="请选择充电模式" clearable>
          <el-option v-for="dict in charge_mode" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="小区筛选" prop="stationId">
        <el-select @change="getList" v-model="queryParams.stationId" placeholder="请选择小区" clearable>
          <el-option v-for="item in stationList" :key="item.stationId" :label="item.stationName"
            :value="item.stationId" />
          <div class="select-pagination">
            <pagination :layout="'total, prev, next'" v-show="total_sts > 0" :total="total_sts"
              v-model:page="querySts.pageNum" v-model:limit="querySts.pageSize" @pagination="getStsList" />
          </div>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList">
      <el-table-column label="订单号" align="center" prop="orderNo" :show-overflow-tooltip="true" />
      <el-table-column label="用户名" align="center" prop="userName" />
      <el-table-column label="手机号" align="center" prop="phonenumber" />
      <el-table-column label="电站名称" align="center" prop="stationName">
        <template #default="scope">
          <span @click="handleStsInfo(scope.row)">
            {{ scope.row.stationName }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="设备名称" align="center" prop="devName">
        <template #default="scope">
          <span @click="handleDevInfo(scope.row)">
            {{ scope.row.devName }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="payStatus">
        <template #default="scope">
          <dict-tag :options="pay_status" :value="scope.row.payStatus" />
        </template>
      </el-table-column>
      <el-table-column label="充电类型" align="center" prop="chargeType">
        <template #default="scope">
          <dict-tag :options="charge_type" :value="scope.row.chargeType" />
        </template>
      </el-table-column>
      <el-table-column label="充电状态" align="center" prop="chargeStatus">
        <template #default="scope">
          <dict-tag :options="charge_status" :value="scope.row.chargeStatus" />
        </template>
      </el-table-column>
      <el-table-column label="通道" align="center" prop="road">
        <template #default="scope">
          <span>第{{ scope.row.road }}路</span>
        </template>
      </el-table-column>
      <el-table-column label="充电模式" align="center" prop="chargeMode">
        <template #default="scope">
          <dict-tag :options="charge_mode" :value="scope.row.chargeMode" />
        </template>
      </el-table-column>
      <el-table-column label="支付渠道" align="center" prop="payChannel">
        <template #default="scope">
          <dict-tag v-if="scope.row.payChannel != null" :options="pay_channel" :value="scope.row.payChannel" />
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="订单金额" align="center" prop="totalAmount">
        <template #default="scope">
          <span>￥{{ scope.row.totalAmount / 100 || 0 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付积分" align="center" prop="payerCoins">
        <template #default="scope">
          <span>{{ scope.row.payerCoins || 0 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实付金额" align="center" prop="payerTotal">
        <template #default="scope">
          <span>￥{{ scope.row.payerTotal / 100 || 0 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="充电消费" align="center" prop="payerTotal">
        <template #default="scope">
          <span>￥{{ scope.row.chargeCost / 100 || 0 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退款金额" align="center" prop="refundTotal">
        <template #default="scope">
          <span>￥{{ scope.row.refundTotal / 100 || 0 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="充电起止时间" align="center" prop="startTime" width="170">
        <template #default="scope">
          <span v-if="scope.row.beginTime">
            {{ scope.row.beginTime }}
            ~
          </span>
          <span v-else>未开始</span>
          <span v-if="scope.row.finishTime">
            {{ scope.row.finishTime }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Document" @click="handleOrderInfo(scope.row)"
            v-hasPermi="['order:charge:query']">详情
          </el-button>
          <!-- <el-button type="primary" link icon="Refresh"
              @click="handleRefresh(scope.row)">刷新订单</el-button> -->
          <el-button link type="primary" icon="Refresh"
            v-if="(scope.row.payStatus == 'SUCCESS' || scope.row.payStatus == 'REFUND') && (scope.row.payerTotal > scope.row.refundTotal)"
            @click="handleRefund(scope.row)">退款
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <DialogRefund ref="refundRef" :reload="getList" />

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script setup name="ChargeOrder">
import { listChargeOrder } from "@/api/trade/charge";
import { listStation } from "@/api/equipment/station";
import DialogRefund from "@/dialog/order/DialogRefund";

const route = useRoute();
const router = useRouter();
const { proxy } = getCurrentInstance();

const { charge_mode, charge_type, charge_status, pay_status, pay_channel } = proxy.useDict("charge_mode", "charge_type", "charge_status", "pay_status", "pay_channel");

const loading = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const showSearch = ref(true);
const total = ref(0);
const infoList = ref([]);
const dateRange = ref("");
const stationList = ref([]);
const total_sts = ref(0);

const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  phonenumber: null,
  userName: null,
  chargeMode: null,
  chargeStatus: null,
  chargeType: null,
  payStatus: null,
});

const querySts = ref({
  pageNum: 1,
  pageSize: 10,
})

// 查询订单信息列表
function getList() {
  loading.value = true;
  if (queryParams.value.chargeMode == "") queryParams.value.chargeMode = null;
  if (queryParams.value.chargeStatus == "")
    queryParams.value.chargeStatus = null;
  if (queryParams.value.chargeType == "") queryParams.value.chargeType = null;
  if (queryParams.value.payStatus == "") queryParams.value.payStatus = null;
  listChargeOrder(proxy.addDateRange(queryParams.value, dateRange.value)).then((response) => {
    infoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function getStsList() {
  listStation(querySts.value).then((response) => {
    stationList.value = response.rows
    total_sts.value = response.total
  })
}

// 搜索按钮操作
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

// 重置按钮操作
function resetQuery() {
  proxy.resetForm("queryForm");
  handleQuery();
}

// 查看设备详情
function handleDevInfo(row) {
  router.push({
    path: "/equipment/dev/info",
    query: { devId: row.devId },
  });
}

function handleStsInfo(row) {
  router.push({
    path: "/equipment/station/info",
    query: { devId: row.devId },
  });
}

// 查看订单详情
function handleOrderInfo(row) {
  router.push({
    path: "/order/charge/info",
    query: { orderNo: row.orderNo },
  });
}

// 退款处理
function handleRefund(row) {
  proxy.$refs["refundRef"].open(row);
}

const beginTime = route.query.beginTime;
const endTime = route.query.endTime;
if (beginTime != undefined && endTime != undefined) {
  dateRange.value = [
    decodeURIComponent(beginTime),
    decodeURIComponent(endTime),
  ];
}
getList();
getStsList()
</script>
